package com.ermao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ermao.mapper.AccountMapper;
import com.ermao.mapper.CategoryMapper;
import com.ermao.pojo.Account;
import com.ermao.pojo.Category;
import com.ermao.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Ermao
 * Date: 2021/9/7 21:38
 */
@Service
public class CategoryServiceImpl implements CategoryService {

	private CategoryMapper categoryMapper;
	private AccountMapper accountMapper;

	@Autowired
	public void setCategoryMapper(CategoryMapper categoryMapper) {
		this.categoryMapper = categoryMapper;
	}

	@Autowired
	public void setAccountMapper(AccountMapper accountMapper) {
		this.accountMapper = accountMapper;
	}

	@Override
	public boolean insertACategory(Integer categoryType, String categoryName) {
		QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
		categoryQueryWrapper.eq("category_name", categoryName);
		Category temp = categoryMapper.selectOne(categoryQueryWrapper);
		if (temp != null) {
			return false;
		}
		Category category = new Category();
		category.setCategoryName(categoryName);
		category.setCategoryType(categoryType);
		return categoryMapper.insert(category) > 0;
	}

	@Override
	public boolean deleteACategory(Integer categoryId) {
		// 将account表中所有被删除的分类ID设置为0
		UpdateWrapper<Account> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("category_id", categoryId);
		Account account = new Account();
		account.setCategoryId(0);
		int update = accountMapper.update(account, updateWrapper);

		return categoryMapper.deleteById(categoryId) > 0;
	}

	@Override
	public boolean updateACategory(Integer categoryId, Integer categoryType, String categoryName) {
		QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("category_name", categoryName);
		Category temp = categoryMapper.selectOne(queryWrapper);
		if (temp != null) {
			return false;
		}
		Category category = new Category();
		category.setCategoryId(categoryId);
		category.setCategoryType(categoryType);
		category.setCategoryName(categoryName);
		return categoryMapper.updateById(category) > 0;
	}

	@Override
	public List<Category> selectAllCategories() {
		QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("category_id", "category_type", "category_name");
		return categoryMapper.selectList(queryWrapper);
	}

	@Override
	public Category selectCategoryByCategoryId(Integer categoryId) {
		QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("category_id", "category_type", "category_name").eq("category_id", categoryId);
		return categoryMapper.selectOne(queryWrapper);
	}

	@Override
	public Category selectCategoryByCategoryName(String categoryName) {
		QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("category_id", "category_type", "category_name").eq("category_name", categoryName);
		return categoryMapper.selectOne(queryWrapper);
	}
}
